package com.cfswt.tasksystem.controller;

import com.cfswt.tasksystem.mapper.*;
import com.cfswt.tasksystem.pojo.dto.CustomDTO;
import com.cfswt.tasksystem.pojo.entity.Contact;
import com.cfswt.tasksystem.pojo.entity.Custom;
import com.cfswt.tasksystem.pojo.entity.CustomContactWrapper;
import com.cfswt.tasksystem.pojo.entity.ReceivingAddress;
import com.cfswt.tasksystem.pojo.vo.CustomVO;
import com.cfswt.tasksystem.pojo.vo.DemandVO;
import com.cfswt.tasksystem.response.JsonResult;
import com.cfswt.tasksystem.security.CustomUserDetails;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/custom")
@Api(tags = "30.客户管理模块")
public class CustomController {

    @Autowired
    private CustomMapper customMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private DemandsMapper demandsMapper;
    @Autowired
    private ReceivingAddressMapper receivingAddressMapper;


    @PostMapping("/add")
    public JsonResult addCustomAndContact(@RequestBody CustomContactWrapper formData,
                                          @AuthenticationPrincipal CustomUserDetails userCurrent) {
        //request.getHeader();
        CustomDTO customDTO = formData.getCustomDTO();
        List<Contact> contactList = formData.getContacts();
        List<ReceivingAddress> receivingAddresses = formData.getReceivingAddresses();
        customDTO.setCreateBy(userCurrent.getNickName());
        System.out.println(userCurrent.getUsername());
        customDTO.setCreateTime(new Date());
        Custom custom = new Custom();
        BeanUtils.copyProperties(customDTO, custom);
        custom.setUpdateTime(new Date());
        customMapper.addCustom(custom);
        System.out.println(custom);
        int companyId = custom.getId();
        for (Contact contact : contactList) {
            contact.setCreateTime(new Date());
            contact.setCompanyId(companyId);
            contactsMapper.addContacts(contact);
        }
        for (ReceivingAddress receivingAddress : receivingAddresses) {
            receivingAddress.setCompanyId(companyId);
            receivingAddressMapper.addReceivingAddress(receivingAddress);
        }
        System.out.println("前端的数据为" + formData);
        return JsonResult.ok(formData);
    }

    @GetMapping("")
    public JsonResult selectCustomDemands() {
        List<CustomVO> customVOS = customMapper.selectCustomVO();
        List<Map<String, Object>> result = new ArrayList<>();
        for (CustomVO customVO : customVOS) {
            Map<String, Object> customMap = new HashMap<>();
            customMap.put("companyName", customVO.getCompanyName());
            customMap.put("createBy", customVO.getCreateBy());
            customMap.put("updateTime", customVO.getUpdateTime());

            List<DemandVO> demandVOList = demandsMapper.selectDemandVOByCompanyName(customVO.getCompanyName());
            List<Map<String, Object>> demandList = new ArrayList<>();
            for (DemandVO demandVO : demandVOList) {
                Map<String, Object> demandMap = new HashMap<>();
                demandMap.put("sn", demandVO.getSn());
                demandMap.put("createTime", demandVO.getCreateTime());
                demandMap.put("head", demandVO.getHead());
                demandMap.put("demandType", demandVO.getDemandType());
                demandMap.put("demandDes", demandVO.getDemandDes());
                demandList.add(demandMap);
            }
            customMap.put("demandHistory",demandList);
            result.add(customMap);
        }
        return JsonResult.ok(result);
    }
    @GetMapping("/company_name")
    public JsonResult getCompanyName(){
        List<String> companyNames = customMapper.selectCompanyName();
        return JsonResult.ok(companyNames);
    }
    @GetMapping("/{companyName}/contacts")
    public JsonResult getCompanyContacts(@PathVariable String companyName){
        Integer id = customMapper.selectIdByCompanyName(companyName);
        List<Contact> contactList = contactsMapper.selectContactsByCompanyId(id);
        return JsonResult.ok(contactList);
    }
    @GetMapping("/{companyName}/address")
    public JsonResult getCompanyAddress(@PathVariable String companyName){
        Integer id = customMapper.selectIdByCompanyName(companyName);
        List<ReceivingAddress> receivingAddresses = receivingAddressMapper.selectReceivingAddressByCompanyId(id);
        System.out.println(receivingAddresses);
        return JsonResult.ok(receivingAddresses);
    }

}
